//==============================================================================
#include "CObjetoAnimado.h"
std::vector<CObjetoAnimado*> CObjetoAnimado::ObjectList;
//==============================================================================
CObjetoAnimado::CObjetoAnimado() {

	for (int i=0;i<4;i++)
        Surf_Objeto[i] = NULL;

	X = 0;
	Y = 0;

	Width 	= 0;
	Height 	= 0;

	Inactive = false;

	Col_X = 0;
	Col_Y = 0;

	Col_Width  = 0;
	Col_Height = 0;

	CurrentIndex=0;
}

//------------------------------------------------------------------------------
CObjetoAnimado::~CObjetoAnimado() {
}

//==============================================================================
bool CObjetoAnimado::OnLoad(char* File, int Width, int Height, int MaxFrames, int index) {
	if((Surf_Objeto[index] = CSurface::OnLoad(File)) == NULL) {
		return false;
	}

	CSurface::Transparent(Surf_Objeto[index], 255, 0, 255);

	this->Width = Width;
	this->Height = Height;
    this->MaxFrames=MaxFrames;
	Anim_Control.MaxFrames = MaxFrames;

    return true;
}

//------------------------------------------------------------------------------
void CObjetoAnimado::OnLoop() {
	OnAnimate();
}
//------------------------------------------------------------------------------
void CObjetoAnimado::OnRender(SDL_Surface* Surf_Display) {

    int aux=0;

    if(Surf_Objeto[CurrentIndex] == NULL || Surf_Display == NULL)
        return;
    CSurface::OnDraw(Surf_Display, Surf_Objeto[CurrentIndex],
            X - CCamera::CameraControl.GetX(), Y - CCamera::CameraControl.GetY(),
            aux * Width, (aux + Anim_Control.GetCurrentFrame()) * Height, Width, Height);
}

//------------------------------------------------------------------------------
void CObjetoAnimado::OnCleanup() {
try{
 for (int i=0 ; i < 4 ;i++) {
        if(Surf_Objeto[i]) {
            SDL_FreeSurface(Surf_Objeto[i]);
            Surf_Objeto[i] = NULL;
        }
    }
}

catch ( std::exception& e){
    std::cout << e.what() << '\n';
}
}
//------------------------------------------------------------------------------
void CObjetoAnimado::OnAnimate() {
	Anim_Control.OnAnimate();
}
//------------------------------------------------------------------------------
bool CObjetoAnimado::OnCollision(CObjetoAnimado* Objeto) {
    return true;
}
//==============================================================================
bool CObjetoAnimado::Collides(int oX, int oY, int oW, int oH) {
    int left1, left2;
    int right1, right2;
    int top1, top2;
    int bottom1, bottom2;

	int tX = (int)X + Col_X;
	int tY = (int)Y + Col_Y;

    left1 = tX;
    left2 = oX;

    right1 = left1 + Width - 1 - Col_Width;
    right2 = oX + oW - 1;

    top1 = tY;
    top2 = oY;

    bottom1 = top1 + Height - 1 - Col_Height;
    bottom2 = oY + oH - 1;


    if (bottom1 < top2) return false;
    if (top1 > bottom2) return false;

    if (right1 < left2) return false;
    if (left1 > right2) return false;

    return true;
}
//==============================================================================
bool CObjetoAnimado::PosValid(int NewX, int NewY) {
	bool Return = true;

	int StartX 	= (NewX + Col_X) / TILE_SIZE;
	int StartY 	= (NewY + Col_Y) / TILE_SIZE;

	int EndX	= ((NewX + Col_X) + Width - Col_Width - 1) 		/ TILE_SIZE;
	int EndY	= ((NewY + Col_Y) + Height - Col_Height - 1)	/ TILE_SIZE;

	for(int iY = StartY;iY <= EndY;iY++) {
		for(int iX = StartX;iX <= EndX;iX++) {
			CTile* Tile = CArea::AreaControl.GetTile(iX * TILE_SIZE, iY * TILE_SIZE);

			if(PosValidTile(Tile) == false) {
				Return = false;
			}
		}
	}

	//if(Flags & ENTITY_FLAG_MAPONLY) {
	//}else{
		for(int i = 0;i < ObjectList.size();i++) {
			if(PosValidObject(ObjectList[i], NewX, NewY,false) == false) {
				Return = false;
			}
		}
	//}
	return Return;
}

//------------------------------------------------------------------------------
bool CObjetoAnimado::PosValidTile(CTile* Tile) {
	if(Tile->TypeID == TILE_TYPE_BLOCK)
		return false;
    else
        return true;
}
//------------------------------------------------------------------------------
bool CObjetoAnimado::PosValidObject(CObjetoAnimado* Objeto, int NewX,
                                    int NewY, bool modoConsulta) {

	if (this != Objeto && Objeto != NULL && Objeto->Inactive == false &&
		Objeto->Collides(NewX + Col_X, NewY + Col_Y, Width - Col_Width - 1,
                    Height - Col_Height - 1) == true
        ) {
            if (!modoConsulta) {
                CObjectCol ObjectCol;
                ObjectCol.ObjetoA = (CObjetoAnimado*)this;
                ObjectCol.ObjetoB = Objeto;
                CObjectCol::ObjectColList.push_back(ObjectCol);
            }
        return false;
	}
	return true;
}

//==============================================================================

